<template>
    <a-form ref='formRef' :label-col='{span : 5}' :model='formData'>
        <a-form-item style='display:none' name='id'>
            <a-input type='hidden' v-model:value='formData.id' />
        </a-form-item>
        <a-form-item label='分类' name='categoryId' :rules="[{required : true, message : '请选择分类'}]">
                <a-tree-select show-search v-model:value='formData.categoryId' :tree-data='categoryOptions' :field-names='{children : "children", label : "name", value : "id"}'></a-tree-select>
        </a-form-item>
        <a-form-item label="语言" name='locale' :rules="[{required : true, message : '请选择语言'}]">
            <a-select :fieldNames="{label : 'name', value : 'code'}" v-model:value='formData.locale' :options='locales'></a-select>
        </a-form-item>
        <a-form-item label='标题' name='title' showCount :maxlength="500" :rules="[{required : true, message : '请填写名称'}]">
            <a-input type='text' v-model:value='formData.title' />
        </a-form-item>
        <a-form-item label='简述' name='description'>
            <a-textarea v-model:value='formData.description' showCount :maxlength="500" />
        </a-form-item>
        <a-form-item label='封面' name='cover'>
            <a-upload list-type='picture-card' :max-count="1" v-model:file-list='coverList' :beforeUpload='beforeUpload' :customRequest='uploadCover' accept="image/png,image/jpeg,image/gif,image/webp">
                <template v-if='coverList.length == 0'>
                    <PlusOutlined />
                </template>
            </a-upload>
        </a-form-item>
        <a-form-item label='链接' name='link' showCount :maxlength="200">
            <a-input type='text' v-model:value='formData.link' />
        </a-form-item>
        <!-- <a-form-item label='文章内容' name='content'>
            <RichEditor v-model="formData.content" :upload-api="upload" />
        </a-form-item> -->
        <a-form-item label='排序' name='sort'>
            <a-input type='number' v-model:value='formData.sort' />
        </a-form-item>
        <a-form-item label='前台显示' name='enable'>
            <a-switch v-model:checked='formData.enable' checked-children='显示' un-checked-children='不禁用'/>
        </a-form-item>
    </a-form>
</template>

<script lang="ts" setup>
import { PlusOutlined } from '@ant-design/icons-vue';
import { optionsApi } from '@/api/help/Category';
import { localeOptionsApi } from "@/api/system/Locale";
    import { saveApi } from '@/api/help/Article';
        import { nextTick, ref } from 'vue';
    import { recursionFn } from '@/utils';
import { useI18nStore } from '@/store/i18n';
import RichEditor from '@/components/RichEditor.vue'
import { locale } from 'dayjs';
import { uploadPicApi } from '@/api/Upload';
import { Modal } from 'ant-design-vue';


    const props = defineProps<{
        row? : any;
        category? : number
    }>()

    const i18n = useI18nStore()

    const formRef = ref()

    const coverList = ref<any[]>([])

    
    const formData = ref<any>({
        id : 0,
        categoryId : '',
        title : '',
        description : '',
        content : '',
        sort : 1,
        locale : '',
        enable : true,
        link : ''

    })

    if(props.row) {
        for(let k in props.row) {
            if(k in formData.value) {
                formData.value[k] = props.row[k]
            }
        }

        if(props.row.cover) {
            coverList.value[0] = {
                status : 'done',
                url : import.meta.env.B_NET_STATIC_URL + props.row.cover
            }
        }

    }

    function uploadCover(e) {
        if(e.file.size > 1024 * 1024) {
            Modal.error({
                title : '图片不能大于1M'
            })
            return
        }



        uploadPicApi(e.file).then(res => {
            coverList.value[0].status = 'done'
            coverList.value[0].url = import.meta.env.B_NET_STATIC_URL + res.data
            formData.value.cover = res.data
        })
    }

    function beforeUpload(file:File, field:FormField) {
        return true
    }


    async function submit() {
        let params  = await formRef.value.validate()
        if('sort' in params && params.sort === '') {
            params.sort = 1
        }


        return saveApi(params)
    }

    function upload(file:File) {
        if(file.size > 1024 * 1024) {
            Modal.error({
                title : '图片不能大于1M'
            })
            return
        }

        
        return uploadPicApi(file)
    }

    //分类
    const categoryOptions = ref<any[]>([])
    const locales = ref<any[]>([])


    optionsApi().then(data => {
        if(data) {
            data.forEach(item => {
                item.name = i18n.t(item.name)
            })
            categoryOptions.value.push(...recursionFn(data, Math.min(...data.map(r => r.parentId)), 'id', 'parentId', 'children'))
        }
    })

    localeOptionsApi().then(data => {
        locales.value = data
    })

    defineExpose({
        submit
    })
</script>